package com.lly.tboc

import android.database.Cursor
import android.os.Build
import android.provider.MediaStore
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.Test
import org.junit.runner.RunWith
import java.io.File

@RunWith(AndroidJUnit4::class)

class PlayerViewTest {
    @Test
    fun scanAudio() {
        val instrumentation = InstrumentationRegistry.getInstrumentation()
        val device = UiDevice.getInstance(instrumentation)
        println(device.productName)
        device.takeScreenshot(File("/sdcard/0.png"))
        val appContext = instrumentation.targetContext
        val audioCollection =
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                MediaStore.Audio.Media.getContentUri(
                    MediaStore.VOLUME_EXTERNAL_PRIMARY
                )
            } else {
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
            }

        appContext.contentResolver.query(
            audioCollection,
            arrayOf(
                MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.DURATION,
                MediaStore.Video.Media.SIZE
            ),
            null,
            null,
            null
        )?.use { cursor ->
            println("Audio count:${cursor.count}")
            println("Audio :${cursor.columnNames.joinToString { "|" }}")
            cursor.moveToFirst()
            val builder = StringBuilder()
            while (cursor.moveToNext()) {
                for (i in 0 until cursor.columnCount) {
                    when (cursor.getType(i)) {
                        Cursor.FIELD_TYPE_BLOB -> {
                            builder.append(cursor.getBlob(i)).append("|")
                        }

                        Cursor.FIELD_TYPE_INTEGER -> {
                            builder.append(cursor.getInt(i)).append("|")
                        }

                        Cursor.FIELD_TYPE_STRING -> {
                            builder.append(cursor.getString(i)).append("|")
                        }

                        Cursor.FIELD_TYPE_FLOAT -> {
                            builder.append(cursor.getFloat(i)).append("|")
                        }

                        Cursor.FIELD_TYPE_NULL -> {
                            builder.append("\t").append("|")
                        }

                        else -> {}
                    }
                }
                builder.append("\n")
            }
            println(builder.toString())
        }
    }
}
